import json
import cv2
import codecs
import numpy as np
import os

#标签路径
img_path = "all_img_50k_fanxiu_20220622_200" #img数据路径
labelme_path = "all_json_50k_fanxiu_20220622_200"   #原始labelme标注数据路径
saved_path = "all_xml_50k_fanxiu_20220622_200"     #保存路径
if not os.path.exists(saved_path):
    os.mkdir(saved_path)
#获取待处理文件
files = [p.split('.json')[0] for p in os.listdir(labelme_path) if '.json' in p]
 
#读取标注信息并写入 xml
for json_file_ in files:
    try:
        json_filename = os.path.join(labelme_path, json_file_) + ".json"
        json_file = json.load(open(json_filename, "r", encoding="utf-8"))
        height, width, channels = cv2.imread(os.path.join(img_path, json_file_) +".jpeg").shape
        if width == 3840 and height == 2160:
            with codecs.open(os.path.join(saved_path, json_file_) + ".xml", "w", "utf-8") as xml:
                xml.write('<annotation>\n')
                xml.write('\t<folder>' + 'Bosch_2D_Detection' + '</folder>\n')
                xml.write('\t<filename>' + json_file_ + ".jpeg" + '</filename>\n')
                xml.write('\t<source>\n')
                xml.write('\t\t<database>The UAV autolanding</database>\n')
                xml.write('\t\t<annotation>UAV AutoLanding</annotation>\n')
                xml.write('\t\t<image>flickr</image>\n')
                xml.write('\t\t<flickrid>NULL</flickrid>\n')
                xml.write('\t</source>\n')
                xml.write('\t<size>\n')
                xml.write('\t\t<width>'+ str(width) + '</width>\n')
                xml.write('\t\t<height>'+ str(height) + '</height>\n')
                xml.write('\t\t<depth>' + str(channels) + '</depth>\n')
                xml.write('\t</size>\n')
                xml.write('\t\t<segmented>0</segmented>\n')
                for multi in json_file["shapes"]:
                    points = np.array(multi["points"])
                    xmin = min(points[:,0])
                    xmax = max(points[:,0])
                    ymin = min(points[:,1])
                    ymax = max(points[:,1])
                    w = xmax - xmin
                    h = ymax - ymin
                    square = w * h
                    label = multi["label"]
                    if xmax <= xmin:
                        pass
                    elif ymax <= ymin:
                        pass
                    elif square >= 64*64:
                        xml.write('\t<object>\n')
                        xml.write('\t\t<name>'+label+'</name>\n')
                        xml.write('\t\t<pose>Unspecified</pose>\n')
                        xml.write('\t\t<truncated>0</truncated>\n')
                        xml.write('\t\t<difficult>0</difficult>\n')
                        xml.write('\t\t<bndbox>\n')
                        xml.write('\t\t\t<xmin>' + str(int(round(xmin))) + '</xmin>\n')
                        xml.write('\t\t\t<ymin>' + str(int(round(ymin))) + '</ymin>\n')
                        xml.write('\t\t\t<xmax>' + str(int(round(xmax))) + '</xmax>\n')
                        xml.write('\t\t\t<ymax>' + str(int(round(ymax))) + '</ymax>\n')
                        xml.write('\t\t</bndbox>\n')
                        xml.write('\t</object>\n')
                        print(json_filename, xmin, ymin, xmax, ymax, label, int(round(w)), int(round(h)), int(round(square)))
                xml.write('</annotation>')
        if width == 1920 and height == 1080:
            with codecs.open(os.path.join(saved_path, json_file_) + ".xml", "w", "utf-8") as xml:
                xml.write('<annotation>\n')
                xml.write('\t<folder>' + 'Bosch_2D_Detection' + '</folder>\n')
                xml.write('\t<filename>' + json_file_ + ".jpeg" + '</filename>\n')
                xml.write('\t<source>\n')
                xml.write('\t\t<database>The UAV autolanding</database>\n')
                xml.write('\t\t<annotation>UAV AutoLanding</annotation>\n')
                xml.write('\t\t<image>flickr</image>\n')
                xml.write('\t\t<flickrid>NULL</flickrid>\n')
                xml.write('\t</source>\n')
                xml.write('\t<size>\n')
                xml.write('\t\t<width>'+ str(width) + '</width>\n')
                xml.write('\t\t<height>'+ str(height) + '</height>\n')
                xml.write('\t\t<depth>' + str(channels) + '</depth>\n')
                xml.write('\t</size>\n')
                xml.write('\t\t<segmented>0</segmented>\n')
                for multi in json_file["shapes"]:
                    points = np.array(multi["points"])
                    xmin = min(points[:,0])
                    xmax = max(points[:,0])
                    ymin = min(points[:,1])
                    ymax = max(points[:,1])
                    w = xmax - xmin
                    h = ymax - ymin
                    square = w * h
                    label = multi["label"]
                    if xmax <= xmin:
                        pass
                    elif ymax <= ymin:
                        pass
                    elif square >= 32*32:
                        xml.write('\t<object>\n')
                        xml.write('\t\t<name>'+label+'</name>\n')
                        xml.write('\t\t<pose>Unspecified</pose>\n')
                        xml.write('\t\t<truncated>0</truncated>\n')
                        xml.write('\t\t<difficult>0</difficult>\n')
                        xml.write('\t\t<bndbox>\n')
                        xml.write('\t\t\t<xmin>' + str(int(round(xmin))) + '</xmin>\n')
                        xml.write('\t\t\t<ymin>' + str(int(round(ymin))) + '</ymin>\n')
                        xml.write('\t\t\t<xmax>' + str(int(round(xmax))) + '</xmax>\n')
                        xml.write('\t\t\t<ymax>' + str(int(round(ymax))) + '</ymax>\n')
                        xml.write('\t\t</bndbox>\n')
                        xml.write('\t</object>\n')
                        print(json_filename, xmin, ymin, xmax, ymax, label, int(round(w)), int(round(h)), int(round(square)))
                xml.write('</annotation>')
    except:
        print('not json')